package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.ClinicalDisease;
import com.ruoyi.web.domain.News;
import com.ruoyi.web.mapper.ClinicalDiseaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.ClinicalMapper;
import com.ruoyi.web.domain.Clinical;
import com.ruoyi.web.service.IClinicalService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * 临床招募Service业务层处理
 *
 * @author drx
 * @date 2024-04-08
 */
@Service
public class ClinicalServiceImpl implements IClinicalService
{
    @Resource
    private ClinicalMapper clinicalMapper;
    @Resource
    private ClinicalDiseaseMapper clinicalDiseaseMapper;

    /**
     * 查询临床招募
     *
     * @param id 临床招募主键
     * @return 临床招募
     */
    @Override
    public Clinical selectClinicalById(Long id)
    {
        Clinical clinical = clinicalMapper.selectClinicalById(id);
        //获取疾病信息
        ClinicalDisease clinicalDisease = new ClinicalDisease();
        clinicalDisease.setClinicalId(id);
        List<ClinicalDisease> list = clinicalDiseaseMapper.selectClinicalDiseaseList(clinicalDisease);
        clinical.setDiseaseList(list);
        return clinical;
    }

    /**
     * 查询临床招募列表
     *
     * @param clinical 临床招募
     * @return 临床招募
     */
    @Override
    public List<Clinical> selectClinicalList(Clinical clinical)
    {
        return clinicalMapper.selectClinicalList(clinical);
    }

    @Override
    public List<Clinical> getClinicalList(Clinical clinical){
        return clinicalMapper.getClinicalList(clinical);
    }

    /**
     * 新增临床招募
     *
     * @param clinical 临床招募
     * @return 结果
     */
    @Override
    public int insertClinical(Clinical clinical)
    {

        clinical.setCreateTime(DateUtils.getNowDate());
        clinical.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        clinical.setCreateName(ShiroUtils.getSysUser().getUserName());
        int i = clinicalMapper.insertClinical(clinical);
        if(null != clinical.getDiseaseList()){
            //保存疾病信息
            List<ClinicalDisease> list = clinical.getDiseaseList();
            list.forEach(item -> {
                ClinicalDisease clinicalDisease = new ClinicalDisease();
                clinicalDisease.setDiseaseId(item.getDiseaseId());
                clinicalDisease.setPid(item.getPid());
                clinicalDisease.setClinicalId(clinical.getId());
                clinicalDiseaseMapper.insertClinicalDisease(clinicalDisease);
            });
        }
        return i;
    }

    /**
     * 修改临床招募
     *
     * @param clinical 临床招募
     * @return 结果
     */
    @Override
    public int updateClinical(Clinical clinical)
    {
        clinical.setUpdateTime(DateUtils.getNowDate());
        clinical.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        clinical.setUpdateName(ShiroUtils.getSysUser().getUserName());
        int i = clinicalMapper.updateClinical(clinical);
        //根据药品id删除疾病
        clinicalDiseaseMapper.deleteClinicalDiseaseByCliId(clinical.getId());
        //保存疾病信息
        if(null != clinical.getDiseaseList()) {
            List<ClinicalDisease> list = clinical.getDiseaseList();
            list.forEach(item -> {
                ClinicalDisease clinicalDisease = new ClinicalDisease();
                clinicalDisease.setDiseaseId(item.getDiseaseId());
                clinicalDisease.setPid(item.getPid());
                clinicalDisease.setClinicalId(clinical.getId());
                clinicalDiseaseMapper.insertClinicalDisease(clinicalDisease);
            });
        }
        return i;
    }

    /**
     * 批量删除临床招募
     *
     * @param ids 需要删除的临床招募主键
     * @return 结果
     */
    @Override
    public int deleteClinicalByIds(String ids)
    {
        return clinicalMapper.deleteClinicalByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除临床招募信息
     *
     * @param id 临床招募主键
     * @return 结果
     */
    @Override
    public int deleteClinicalById(Long id)
    {
        return clinicalMapper.deleteClinicalById(id,ShiroUtils.getSysUser().getLoginName());
    }



}
